package com.testStream;


import org.junit.Assert;
import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author cxt
 * @date 2020/8/26
 * 合并区间练习
 */
public class MergeIntervals56 {
    @Test
    public void mergeIntervalsTest() {
        int[][] intervals = {{1, 3}, {8, 10}, {2, 6}, {15, 18}};
        int[][] merge = merge(intervals);
        System.out.println(Arrays.deepToString(merge));
        Assert.assertArrayEquals(merge[0], new int[]{1, 6});
    }

    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> a[0] - b[0]); // 排序
        List<int[]> list = new ArrayList<>();
        int length = intervals.length;
        if (length <= 1) {
            return intervals;
        }
        int[] curInterval = intervals[0];
        for (int i = 1; i < length; i++) {
            if (intervals[i][0] <= curInterval[1]) { // 数组是可以直接比对大小的
                curInterval[1] = Math.max(intervals[i][1], curInterval[1]); // 数组取最大的
            } else {
                list.add(curInterval);
                curInterval = intervals[i];
            }
        }
        list.add(curInterval);
        int size = list.size();
        int[][] ret = new int[size][];
        for (int i = 0; i < size; i++) {
            ret[i] = list.get(i);
        }
        return ret;
    }

    @Test
    public void lastDate() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse("2020-11-05");
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
        cal.set(Calendar.DAY_OF_YEAR, last);

        System.out.println(sdf.format(cal.getTime()));
    }

    @Test
    public void sss() {
        String str = "2020-10-12 00:00:00.0";
        String[] s = str.split(" ");
        System.out.println(s[0]);
    }

    @Test
    public void sss1() {
        String str1 = null;
        String str = "null";
        String sss = str1 + str;
//        Integer integer = Integer.valueOf(str);
        System.out.println(sss);
    }

    public void threadOne() {

    }


}